package com.qgzj.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qgzj.common.vo.ContentDetailRequest;
import com.qgzj.common.vo.ContentRequest;
import com.qgzj.entity.Content;
import com.qgzj.entity.ContentDetail;
import com.qgzj.mapper.ContentDetailMapper;
import com.qgzj.mapper.ContentMapper;
import com.qgzj.response.PageQueryRes;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContentDetailService {

    @Resource
    ContentDetailMapper contentDetailMapper;


    private Logger logger = LoggerFactory.getLogger(ContentDetailService.class);

    public PageQueryRes<ContentDetail> selectContentDetail(ContentDetailRequest contentDetailReq) {
        int page = contentDetailReq.getPage();
        int pageSize = contentDetailReq.getPageSize();
        PageQueryRes res = new PageQueryRes();
        res.setPage(page);
        res.setPageSize(pageSize);
        String materialNo = contentDetailReq.getMaterialNo();
        String volumeNo = contentDetailReq.getVolumeNo();
        String contentNo = contentDetailReq.getContentNo();
        EntityWrapper<ContentDetail> wrapper = new EntityWrapper<>();
        wrapper.eq("material_no", materialNo);
        wrapper.eq("volume_no", volumeNo);
        wrapper.eq("content_no", contentNo);
        wrapper.orderBy("id",false);
        List<ContentDetail> contents = contentDetailMapper.selectPage(new Page<>(page, pageSize),wrapper);
        res.setCount(contentDetailMapper.selectCount(wrapper));
        res.setList(contents);
        return res;
    }



    public void insertContentDetail(ContentDetailRequest contentDetailReq) {
        ContentDetail content = transContentDetailReq2ContentDetail(contentDetailReq);
        content.setCreateTime(new Date());
        content.setUpdateTime(new Date());
        Integer insert = contentDetailMapper.insert(content);
    }

    public void updateContentDetail(ContentDetailRequest contentDetailReq) {
        ContentDetail content = transContentDetailReq2ContentDetail(contentDetailReq);
        content.setUpdateTime(new Date());
        String volumeNo = content.getVolumeNo();
        String materialNo = content.getMaterialNo();
        String contentNo = content.getContentNo();
        EntityWrapper<ContentDetail> wrapper = new EntityWrapper<>();
        wrapper.eq("volume_no", volumeNo);
        wrapper.eq("material_no", materialNo);
        wrapper.eq("content_no", contentNo);
        Integer update = contentDetailMapper.update(content, wrapper);
    }

    public void deleteContentDetail(ContentDetailRequest contentDetailReq) {
        String volumeNo = contentDetailReq.getVolumeNo();
        String materialNo = contentDetailReq.getMaterialNo();
        String contentNo = contentDetailReq.getContentNo();
        EntityWrapper<ContentDetail> wrapper = new EntityWrapper<>();
        wrapper.eq("volume_no", volumeNo);
        wrapper.eq("material_no", materialNo);
        wrapper.eq("content_no", contentNo);
        Integer update = contentDetailMapper.delete(wrapper);
    }

    public void deleteByContentNo(String contentNo) {
        EntityWrapper<ContentDetail> wrapper = new EntityWrapper<>();
        wrapper.eq("content_no", contentNo);
        Integer update = contentDetailMapper.delete(wrapper);
    }

    public ContentDetail transContentDetailReq2ContentDetail(ContentDetailRequest contentDetailReq){
        ContentDetail contentDetail = new ContentDetail();
//        if(StringUtils.isNotBlank(contentDetailReq.getContentNo())){
//            contentDetail.setContentNo(contentDetailReq.getContentNo());
//        }
//        if(StringUtils.isNotBlank(contentDetailReq.getVolumeNo())){
//            contentDetail.setVolumeNo(contentDetailReq.getVolumeNo());
//        }
//        if(StringUtils.isNotBlank(contentDetailReq.getMaterialNo())){
//            contentDetail.setMaterialNo(contentDetailReq.getMaterialNo());
//        }
//        if(StringUtils.isNotBlank(contentDetailReq.getVoiceAddress())){
//            contentDetail.setVoiceAddress(contentDetailReq.getVoiceAddress());
//        }
//        if(StringUtils.isNotBlank(contentDetailReq.getContentText())){
//            contentDetail.setContentText(contentDetailReq.getContentText());
//        }
//        contentDetail.setCreateTime(new Date());
//        contentDetail.setUpdateTime(new Date());
        BeanUtils.copyProperties(contentDetailReq,contentDetail);
        return contentDetail;
    }

}
